﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HPortal.Lib
{
    public class StringHelper
    {
        public const string CRLF = "\r\n";
        public static string CaptionLanguage(string vni, string eng)
        {
            try
            {
                bool isEN = (bool)System.Web.HttpContext.Current.Session["isEN"];
                if (isEN)
                    return eng;
                return vni;
            }
            catch (Exception ex)
            {
                return vni;
            }
        }
        # region ArrayString Code

        public static string ArrayStringToString(string[] StringArr, string separatorString)
        {
            return (StringArr != null) ? (StringArr.Length > 1 ? string.Join(separatorString, StringArr) : StringArr[0]) : string.Empty;
        }

        public static string[] StringToArrayString(string strT, string separatorString)
        {
            while (strT.Substring(strT.Length - 1, 1) == separatorString)
                strT = strT.Substring(0, strT.Length - 1);
            if (strT.IndexOf(separatorString) < 0)
                strT += separatorString;
            return strT.Split(separatorString.ToCharArray());
        }

        # endregion

        # region ViewStrike Code
        public static string ViewStrike(object TextView, object bolLock)
        {
            return ViewStrike(TextView, bolLock, false);
        }

        public static string ViewStrike(object TextView, object bolLock, object bolKeep)
        {
            string strtext = TextView.ToString();
            if ((bool)bolKeep)
                return ViewStrike(strtext);
            if (!(bool)bolLock)
                return strtext;
            return ViewStrike(FormatString("<FONT color=\"#ff0000\">", strtext, "</FONT>"));
        }

        public static string ViewStrike(string strtext)
        {
            return FormatString("<STRIKE>", strtext, "</STRIKE>");
        }

        public static string FormatString(string strFirstFormat, string strText, string strLastFormat)
        {
            return strFirstFormat + strText + strLastFormat;
        }

        # endregion

        # region SplitFullName Code

        public static void SplitFullName(string FullName_, out string FirstName_, out string LastName_)
        {
            int space = FullName_.LastIndexOf(" ");
            FirstName_ = string.Empty;
            LastName_ = string.Empty;
            if (space > 0)
            {
                FirstName_ = FullName_.Substring(0, space);
                LastName_ = FullName_.Substring(space, FullName_.Length - space);
            }
            else
                LastName_ = FullName_;
        }
        # endregion

        # region StringCut Code

        //Cut string 0 to length - 1
        public static string StringCut(string strT)
        {
            return strT.Substring(0, strT.Length - 1);
        }

        public static string StringLeft(string strT, int Left)
        {
            return strT.Substring(Left, strT.Length);
        }

        public static string StringRight(string strT, int Right)
        {
            return strT.Substring(0, strT.Length - Right);
        }

        # endregion

        # region ConvertDate Code

        //Convert DateTime to String
        public static string ConvertDateTime(DateTime dt)
        {
            return dt.ToString("dd/MM/yyyy");
        }
        //Convert DateTime to VN String
        public static string ConvertDateTimeToVN(DateTime dt)
        {
            return dt.ToString("'ngày' dd 'tháng' MM 'năm' yyyy");
        }
        public static string ConvertDateTimeToVN1(DateTime dt)
        {
            string n = "";
            switch (dt.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    n = "Chủ nhật";
                    break;
                case DayOfWeek.Monday:
                    n = "Thứ hai";
                    break;
                case DayOfWeek.Tuesday:
                    n = "Thứ ba";
                    break;
                case DayOfWeek.Wednesday:
                    n = "Thứ tư";
                    break;
                case DayOfWeek.Thursday:
                    n = "Thứ năm";
                    break;
                case DayOfWeek.Friday:
                    n = "Thứ sáu";
                    break;
                case DayOfWeek.Saturday:
                    n = "Thứ bảy";
                    break;
            }
            string[] d = new string[] { "Thứ hai", "Thứ ba", "Thứ tư", "Thứ năm", "Thứ sáu", "Thứ bảy", "Chủ nhật" };
            return n + ", ngày " + dt.Day + " tháng " + dt.Month + " năm " + dt.Year;
        }

        public static string ConvertDateTimeToEN(DateTime dt)
        {
            return dt.ToString("'Today is ' MM - dd - yyyy");
        }
        public static string ConvertDateTimeToEN1(DateTime dt)
        {
            return dt.DayOfWeek + dt.ToString("',' MM - dd - yyyy");
        }
        # endregion

        # region ConvertNumberString Code

        public static string ConvertNumberToString(double Value_)
        {
            try
            {
                string[] ConstNumberString = new string[]{" không", " một", " hai", " ba", 
																" bốn", " năm", " sáu", " bảy", " tám", " chín", " mươi"," mười",
																" mốt"," linh", " trăm", " nghìn"," triệu"," tỷ"};

                if (Value_ == 0)
                    return ConstNumberString[0];

                //Number0 = Count number 0
                double Value_tmp = Value_;
                int Number0 = 0;
                int j = (int)Value_tmp % 10;
                while (j == 0 && Value_tmp != 0)
                {
                    Number0++;
                    Value_tmp = (int)Value_tmp / 10;
                    j = (int)Value_tmp % 10;
                }

                int CountNumber = 0;
                //Convert Number To StringTmp Arr
                string[] Number = new string[(Value_.ToString()).Length + 1];

                while (Value_ != 0)
                {
                    int i = (int)Value_ % 10;
                    Number[CountNumber] = ConstNumberString[i];
                    CountNumber++;
                    Value_ = (int)Value_ / 10;
                }

                //Convert Number To String
                string result = string.Empty;
                int FirstCout = CountNumber;
                while (CountNumber - Number0 != 0 && CountNumber != 0)
                {
                    switch (CountNumber % 3)
                    {
                        case 0:
                            if (!(Number[CountNumber - 1] == ConstNumberString[0] && Number[CountNumber - 2] == ConstNumberString[0] && Number[CountNumber - 3] == ConstNumberString[0]))
                                result += Number[CountNumber - 1] + ConstNumberString[14]; /*" trăm"*/
                            break;

                        case 1:
                            if (!(Number[CountNumber - 1] == ConstNumberString[0]))
                                if (Number[CountNumber] != ConstNumberString[1] && Number[CountNumber - 1] == ConstNumberString[4] && FirstCout != 1 && FirstCout != CountNumber)
                                    result += ConstNumberString[4];
                                else
                                    if (Number[CountNumber] != ConstNumberString[0] && Number[CountNumber - 1] == ConstNumberString[5] && FirstCout != 1 && FirstCout != CountNumber)
                                        result += Number[CountNumber - 1];
                                    else
                                        if (CountNumber == 1 && Number[CountNumber - 1] == ConstNumberString[1])
                                            result += ConstNumberString[12]; // " mốt";
                                        else
                                            result += Number[CountNumber - 1];
                            break;

                        case 2:
                            if ((Number[CountNumber - 1] == ConstNumberString[1] && FirstCout == CountNumber) || (Number[CountNumber - 1] == ConstNumberString[1] && Number[CountNumber] == ConstNumberString[0]) || (Number[CountNumber - 1] == ConstNumberString[12]/* " mốt"*/))
                                result += ConstNumberString[11]; /* " mười"*/
                            else if (Number[CountNumber - 1] == ConstNumberString[0] && Number[CountNumber - 2] != ConstNumberString[0])
                                result += " linh";
                            else if (!(Number[CountNumber - 1] == ConstNumberString[0] && Number[CountNumber - 2] == ConstNumberString[0]))
                                if (Number[CountNumber - 1] != ConstNumberString[1])
                                    result += Number[CountNumber - 1] + ConstNumberString[10]; /* " mươi"*/
                                else
                                    result += ConstNumberString[11]; /* " mười"*/
                            break;
                    }

                    switch (CountNumber)
                    {
                        case 4:
                            if (!(Number[CountNumber - 1] == ConstNumberString[0] && Number[CountNumber] == ConstNumberString[0] && Number[CountNumber + 1] == ConstNumberString[0]))
                                result += ConstNumberString[15]; /* " nghìn"*/
                            break;

                        case 7:
                            if (!(Number[CountNumber - 1] == ConstNumberString[0] && Number[CountNumber] == ConstNumberString[0] && Number[CountNumber + 1] == ConstNumberString[0]))
                                result += ConstNumberString[16]; /* " triệu"*/
                            break;

                        case 10:
                            if (!(Number[CountNumber - 1] == ConstNumberString[0] && Number[CountNumber] == ConstNumberString[0] && Number[CountNumber + 1] == ConstNumberString[0]))
                                result += ConstNumberString[17]; /* " tỷ"*/
                            break;
                    }

                    CountNumber--;
                }

                switch (Number0)
                {
                    case 4:
                    case 5:
                        result += ConstNumberString[15] /* " nghìn"*/;
                        break;

                    case 7:
                    case 8:
                        result += ConstNumberString[16] /* " triệu"*/;
                        break;

                    case 10:
                    case 11:
                        result += ConstNumberString[17] /* " tỷ"*/;
                        break;
                }

                return result.Substring(1);
            }
            catch (Exception ex)
            {
                throw new Exception("Error StringHelper! - " + ex.Message);
            }
        }
        # endregion

        # region ConvertString Code
        //Convert TCVN3 to Unicode
        public static string ConvertTcvn3ToUnicode(string ConvertStr)
        {
            return DatabaseHelper.ConvertTcvn3ToUnicode(ConvertStr);
        }

        //Convert Unicode to TCVN3
        public static string ConvertUnicodeToTcvn3(string ConvertStr)
        {
            return DatabaseHelper.ConvertUnicodeToTcvn3(ConvertStr);
        }

        # endregion

        # region Encoding AND Decoding

        # region Encoding AND Decoding Code

        public static string strEn = "^&*(]|+=1290a6bc-}[{+!@#$%dejkno)~fghi.,?<78lm>/'pqtuv345xyz_¸µ¶·¹¨¾»¼½Æ©ÊÇÈÉËÐÌÎÏÑªÕÒÓÔÖÝ×ØÜÞãßáâä«èåæçé¬íêëìîóïñòô­øõö÷ùýúûüþ®¸µ¶·¹¡¾»¼½Æ¢ÊÇÈÉËÐÌÎÏÑ£ÕÒÓÔÖÝ×ØÜÞãßáâä¤èåæçé¥íêëìîóïñòô¦øõö÷ùýúûüþ§";
        public static string strDe = "-+!@<}xuq2>/']4ov#$%^&*()~.,?tpn6805mk[{|+=bzigecayl1973jhfd_áàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđÁÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬÉÈẺẼẸÊẾỀỂỄỆÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴĐ";

        # endregion

        //Convert String
        private static string ConvertEncodingDecoding(string _strCon1, string _strCon2, string _ConvertStr)
        {
            string strTemp = string.Empty;
            int intPos;
            for (int i = 0; i < _ConvertStr.Length; i++)
            {
                intPos = _strCon1.IndexOf(_ConvertStr[i]);
                if (intPos >= 0)
                    strTemp += _strCon2[intPos];
                else
                    strTemp += _ConvertStr[i];
            }
            return strTemp;
        }

        //Convert Decoding (Giai ma hoa)
        public static string ConvertDecoding(string ConvertStr)
        {
            if (ConvertStr.Length == 0)
                return ConvertStr;
            return ConvertEncodingDecoding(strEn, strDe, ConvertStr);
        }

        //Convert Encoding (Ma hoa)
        public static string ConvertEncoding(string ConvertStr)
        {
            if (ConvertStr.Length == 0)
                return ConvertStr;
            return ConvertEncodingDecoding(strDe, strEn, ConvertStr);
        }

        # endregion
    }
}